package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.myExceptions.AssociatedException;
import com.itheima.pojo.PhysicalExam;
import com.itheima.service.InterventionService;
import com.itheima.service.PhysicalExamService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 健康干预管理
 */
@RestController
@RequestMapping("/intervention")
public class InterventionController {

    @Reference
    private PhysicalExamService physicalExamService;

    @Reference
    private InterventionService interventionService;

    /**
     * 分页查询体检结果
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return physicalExamService.pageQuery(queryPageBean);
    }

    /**
     * 健康干预
     * @param foodIds
     * @param sportIds
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody PhysicalExam physicalExam, Integer[] foodIds, Integer[] sportIds) {
        try {
            //调用服务层
            interventionService.add(physicalExam, foodIds, sportIds);
        } catch (Exception e) {
            if (e instanceof AssociatedException){
                return new Result(false,"该结果已干预，无法重新干预");
            }
            e.printStackTrace();
            //干预失败
            return new Result(false, MessageConstant.ADD_INTERVENTION_FAIL);
        }
        //干预成功
        return new Result(true, MessageConstant.ADD_INTERVENTION_SUCCESS);
    }

}
